<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <canvas id="canvas" width="1000" height="1000"></canvas>
  <script>
    const canvasEle = document.getElementById('canvas')
    const ctx = canvasEle.getContext('2d')
    const img = new Image()
    img.src = 'img/test.jpeg'
    img.onload = function () {
      // ctx.drawImage(img, 100, 100) // 渲染图片，从(100, 100)位置开始显示
      // ctx.drawImage(img, 100, 100, 300, 300) // 限制图片宽高
      // 从图片的(100, 100)裁剪宽高为300的内容并渲染
      ctx.drawImage(img, 100, 100, 300, 300, 100, 100, 300, 300)
    }
  </script>
</body>

</html>